<template>
  <div class="contact" v-if="is_show_select">
    <van-nav-bar
      :title="title"
      left-text="返回"
      left-arrow
      @click-left="hide"
      class="title"
    />
    <van-search
      v-model="search_value"
      shape="round"
      placeholder="请输入搜索关键词"
      class="search"
      clearable
      @search="search(search_value)"
      @clear="search('')"
    />
    <div class="list_box" :style="type == 'multiple'? ' bottom: 54px;' : ' bottom: 0px;'">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="我是有底线的"
        @load="onLoad"
      >
        <van-checkbox-group v-model="result">
          <div v-for="item in contact_list" :key="item[option.value]" class="item van-hairline--bottom" @click="selecte(item)">
            <div class="lable_box">
              <div class="lable">
                {{item[option.lable]}}
              </div>
              <van-checkbox  class="checkbox" :name="item[option.value]"  checked-color="#07c160"></van-checkbox>
            </div>
            <div v-for="name in name_list" :key="name" class="name_box">
              {{item[name]}}
            </div>
          </div>
         </van-checkbox-group>
      </van-list>
    </div>
    <div class="btn_box van-hairline--top" v-if="type == 'multiple'">
      <van-button type="info" class="btn" block @click="confirm" :disabled="!this.selected_list.length">
        确定 
      </van-button>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      search_value:'',
      is_show_select: false,
      title: '',
      type: 'multiple',
      contact_list:[],
      loading: false,
      finished: false,
      selected_list:[],
      result:[], // 已选择状态,留作为修改回填的状态显示,
      next:null,
      page:0,
      is_deliverTicket:'',
      option:{},
      name_list:[]
    }
  },
  mounted(){
    // dom渲染成功后 监听popstate 事件
    window.addEventListener('popstate',e => {
      if(this.is_show_select){
        this.is_show_select = !this.is_show_select
      }
    },false)
  },
  beforeDestroy(){
    // 页面销毁前 移除popstate 事件
    window.removeEventListener('popstate',e => {
      if(this.is_show_select){
        this.is_show_select = !this.is_show_select
      }
    },false)
  },
  methods:{
    // 关闭已选列表
    hideSelected(){
      this.$refs.selected_contact.hide()
      this.show_selected_contact = false
    },
    // 显示已选择的列表
    showSelected(){
      this.show_selected_contact = true
      this.$refs.selected_contact.show(this.selected_list)
    },
    // 已选择的列表变更
    selectedList(list){
      this.result = []
      this.selected_list = []
      list.forEach(item => {
        this.selected_list.push(item)
        this.result.push(item[this.option.value])
      })
    },
    // 确认
    confirm(){
      this.hide()
      this.$emit('selecteList',{list:this.selected_list,option:this.option})
    },
    // 选择
    selecte(item){
      if(this.type == 'single'){
        // 单选
        if(item[this.option.value] == this.result[0]){
          // 删除
          this.result = []
          this.selected_list = []
        }else{
          // 添加
          this.result = [item[this.option.value]]
          this.selected_list = [item]
          this.confirm()
        } 
      }else{
        // 多选
        if(this.result.includes(item[this.option.value])){
          // 删除
          this.selected_list.forEach((item,index) => {
            if (this.selected_list[index].username == item[this.option.value]){
              this.selected_list.splice(index,1);
            }
          });
          this.result.forEach( (id,index) => {
            if (this.result[index] == item[this.option.value]){
              this.result.splice(index,1);
            }
          })
        }else{
          // 添加
          this.result.push(item[this.option.value])
          this.selected_list.push(item)
        }
      }
    },
    // 显示
    show(opt={}){
      this.selected_list = []
      this.result = []
      if(opt.option.type){
        this.option = opt.option
        this.name_list = opt.option.name_list || []
        console.log(this.name_list)
        this.type = opt.option.type
      }
      if(opt.title){
        this.title = opt.title
      }
      if(opt.is_deliverTicket){
        this.is_deliverTicket = opt.is_deliverTicket
      }
      if(opt.list){
        opt.list.forEach(item => {
           this.selected_list.push(item)
           this.result.push(item[this.option.value])
        })
      }
      this.is_show_select = true
      window.history.pushState(null,null,'#')
    },
    // 关闭
    hide(opt={}){
      this.is_show_select = false
      if(this.is_deliverTicket != 'deliverTicket'){
        this.$router.go(-1)
      }
    },
    search(search_value){
      this.finished = false;
      this.page = 0
      this.contact_list=[]
      this.onLoad(search_value)
    },
    // 加载列表
    onLoad(search_value=this.search_value) {
      console.log(this.option)
      this.loading = true;
      this.page ++ 
      this.$store.dispatch(
        'get_select_list',{
        url:this.option.url,
        params:{
          page:this.page,
          search:search_value
        }}).then(res=>{
        console.log(res)
        res.data.results.forEach(item=>{
        this.contact_list.push(item)
        })
        this.loading = false;
        this.next = res.data.next
        if(!res.data.next){
          this.finished = true;
        }
      }).catch(err=>{
        this.finished = true;
      })
    }
  }
}
</script>

<style lang="less" scoped>
.contact{
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  z-index: 9999;
  .title{
    position: absolute;
    height: 46px;
    top: 0;
    width: 100%;
  }
  .search{
    position: absolute;
    height: 54px;
    top: 46px;
    width: 100%;
  }
  .list_box{
    position: absolute;
    top: 100px;
    width: 100%;
    overflow: auto;
    .item{
      min-height: 50px;
      padding: 0px 15px;
      line-height: 50px;
      box-sizing: border-box;
    }
  }
  .btn_box{
    position: absolute;
    height: 54px;
    bottom: 0;
    width: 100%;
    line-height: 54px;
    color: rgb(58, 148, 250);
    .span{
      margin-left: 15px;
      font-size: 14px;
    }
    .btn{
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
      width: 80px;
      height: 32px;
      line-height: 32px;
      font-size: 12px;
      padding: 0;
    }
  }
}
.show_selected_contact{
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.541);
  z-index: 98;
}
.name_box{
  width: 100%;
  display: block;
  color: #888;
  font-size: 13px;
}
.lable_box{
  display: flex;
  .lable{
    flex: 1;
    font-size: 14px;
  }
}
</style>